package com.an_yang_gang_yie.an_yang_gang_yie.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class EmailUtil {

    private  Logger logger = LoggerFactory.getLogger(getClass());
    public JavaMailSenderImpl JavaMailSender() {
//        mail:
//        host: smtp.qq.com
//        username: 229236940@qq.com
//        password: rgukfkzyzqsxbhec
//        default-encoding: utf-8
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.qq.com");
        mailSender.setUsername("229236940@qq.com");
        mailSender.setPassword("rgukfkzyzqsxbhec");
        mailSender.setPort(587);
        return mailSender;
    }
    /**
     * 发送邮箱验证码
     * @param toMail  目标邮箱
     */

    public  void sendSimpleMail(String toMail,String code){
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom("229236940@qq.com");
        simpleMailMessage.setTo(toMail);
        simpleMailMessage.setSubject("主题：邮箱验证");
        simpleMailMessage.setText("验证码："+code);
       try {
           JavaMailSenderImpl javaMailSender = JavaMailSender();
           javaMailSender.send(simpleMailMessage);
       }catch (MailException e){
           logger.error(e.getMessage());
           e.printStackTrace();
       }
    }
}
